package com.qxsx.midi.activity.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/9/28.
 */
public class MeReceivingAlterAddressActivity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private Button btn_conserve;
    private EditText et_name, et_phone, et_mail, et_address;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_address);
        setTitle("修改收货地址");
        setNavBtn(R.mipmap.back, 0);
        findById();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String address = (String) SPUtils.get(AppContext.getContext(), "address", "");
        String consignees_name = (String) SPUtils.get(AppContext.getContext(), "consignees_name", "");
        String consignees_phone = (String) SPUtils.get(AppContext.getContext(), "consignees_phone", "");
        String postcode = (String) SPUtils.get(AppContext.getContext(), "postcode", "");
        et_name.setText(consignees_name);
        et_phone.setText(consignees_phone);
        et_mail.setText(postcode);
        et_address.setText(address);
    }

    private void findById() {
        btn_conserve = (Button) findViewById(R.id.btn_conserve);
        btn_conserve.setOnClickListener(this);

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_mail = (EditText) findViewById(R.id.et_mail);
        et_address = (EditText) findViewById(R.id.et_address);

    }

    //添加地址接口
    private void newaddress() {
        customProgress = CustomProgress.show(MeReceivingAlterAddressActivity.this, "请稍后....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("user_id", user_id);
            object.put("consignees_name", et_name.getText().toString().trim());
            object.put("consignees_phone", et_phone.getText().toString().trim());
            object.put("postcode", et_mail.getText().toString().trim());
            object.put("address", et_address.getText().toString().trim());
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8052", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                customProgress.dismiss();
            }
        });

    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_conserve:
                if (TextUtils.isEmpty(et_name.getText().toString())) {
                    ShowToast("请输入您的姓名");
                } else {
                    if (TextUtils.isEmpty(et_phone.getText().toString()) || !Tools.isMobileNO(et_phone.getText().toString())) {
                        ShowToast("请输入正确的手机号");

                    } else {
                        if (TextUtils.isEmpty(et_mail.getText().toString()) || !Tools.isZipNO(et_mail.getText().toString())) {
                            ShowToast("请输入正确的邮编");

                        } else {
                            if (TextUtils.isEmpty(et_address.getText().toString())) {
                                ShowToast("请输入您的地址");

                            } else {
                                newaddress();
                                finish();
                            }
                        }
                    }

                }


                break;
        }
    }
}
